const path = require("path");
const {createGoods, allGoods} = require("../service/goods.server")

class GoodsController {
    async upload(ctx, next) {
        const {file} = ctx.request.files
        const fileType = ['image/jpeg', 'image/png']
        if (file) {
            if (!fileType.includes(fileType.type)) return ctx.app.emit('err', {
                code: 1102,
                message: "不支持的文件格式"
            }, ctx)
            ctx.body = {
                code: 200,
                message: "商品上传成功",
                result: {
                    goods_imgs: path.basename(file.filepath)
                }
            }
        } else {
            return ctx.app.emit('err', {
                code: 1100,
                message: "商品上传失败"
            }, ctx)
        }
    }

    async create(ctx) {
        //     操作数据库
        try {
            const userId = ctx.state.user.id
            userId && Object.assign(ctx.request.body, {userId})
            const res = await createGoods(ctx.request.body)
            ctx.body = {
                code: 200,
                message: "商品上传成功",
                result: res

            }
        } catch (e) {
            console.log(e)
        }

    }

    async upade(ctx) {
        console.log(ctx.request.body.id)
        console.log(ctx.params.id)
    }

    async allGoods(ctx) {
        try {
            const res = await allGoods()
            ctx.body = {
                code: 200,
                message: "商品上传成功",
                result: res

            }
        } catch (e) {

        }
    }

//     测试
 async    wxTest(ctx){
        ctx.body = {
            code: 200,
            message: "商品上传成功",

        }
    }
}

module.exports = new GoodsController()